library(knitr)
library(data.table)
library(tidyverse)
library(leaflet)
library(lubridate)
crime_raw <- read.csv("RMS_Crime_Incidents.csv")
Using DT
data.table(crime_raw, options = list(pageLength = 5, scrollX = '400px'))
crime_raw %>%
group_by(year) %>%
summarize(count = n()) %>%
arrange(desc(year))
This dataset has data that goes as far back as 1915 but it only has accurate data since 2017 it appears. The limitation of this data will only allow us to accurately analyze the years 2017 through 2020.
Renaming the incident_timestamp column for clarity and removing the time from the timestamp because there is already another column with that information. Filtering the data set because of the limitation. Adding extra columns that will allow for easier analysis. Getting rid of columns with the same crime_id and arrest charge because that would mean it was a duplicate entry.
crime <- crime_raw %>%
rename(date = incident_timestamp) %>%
mutate(date = ymd_hms(date)) %>%
mutate(date = date(date)) %>%
filter(year > 2016 & year <= 2020) %>%
add_column(crime_type = NA) %>%
add_column(month = NA) %>%
mutate(month = month(date, TRUE)) %>%
group_by(crime_id, arrest_charge) %>%
filter(!n()>1) %>%
ungroup()
The two types of crime that are being analyzed are property and violent crimes. In order to categorize these crimes we need to view the unique offense categories.
unique_offenses <- unique(crime[c("offense_category")])
data.table(unique_offenses)
From here we can create two vectors to distinguish the property and violent crimes.
#violent_crime <- scan("violent_crimes.txt", what ="character")
violent_crime <- c("ASSAULT","AGGRAVATED ASSAULT","SEXUAL ASSAULT","HOMICIDE","KIDNAPPING")
property_crime <- c("ROBBERY","LARCENY","ARSON","STOLEN VEHICLE","BURGLARY","DAMAGE TO PROPERTY","STOLEN PROPERTY")
Now we can update the crime_type column to show the crime_type. If it is neither a property or violent crime we will just classify it as other.
`%notin%` <- Negate(`%in%`)
crime <- within(crime, {
crime_type[offense_category %notin% (violent_crime) || (property_crime)] = "other"
crime_type[offense_category %in% (violent_crime)] = "violent"
crime_type[offense_category %in% (property_crime)] = "property" })
The total of the type of reported crime in the 4 year period.
crime %>%
group_by(crime_type) %>%
summarize(count = n())
crime %>%
filter(crime_type == "other") %>%
distinct(offense_category)
Some of these offenses aren’t dependent on the variables we wanted to analyse. In order to get a clear analysis we will be looking at the data that is either classified as violent or property crime.
crime <- crime %>%
filter(crime_type != "other")
Create a pie chart on the offense category. Here we can see the breakdown of offenses.
dt <- crime %>%
group_by(offense_category) %>%
summarise(count = n()) %>%
arrange(count, .by_group = TRUE)
blank_theme <- theme_minimal()+
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.border = element_blank(),
panel.grid=element_blank(),
axis.ticks = element_blank(),
plot.title=element_text(size=14, face="bold")
)
dt %>% ggplot(aes(x="", y=count, fill= offense_category))+
geom_bar(width = 1, stat = "identity")+
coord_polar("y", start = 0)+
blank_theme +
theme(axis.text.x=element_blank())+
labs(title = "Detroit Crime by Offense Category(2017-2020)")
Using the same data but making it bar chart
dt %>%
ggplot(aes(x = reorder(offense_category,count), y = count)) +
geom_col(fill = "orange")+
coord_flip()+
labs(title = "Crimes in Detroit 2017-2020", x = "Offense Category", y = "Crime Count")
Display all the offenses in 2020. Click the icons to show details.
data <- crime %>% filter(year == 2020)
data$popup <- paste("<b>Incident #: </b>", data$crime_id, "<br>", "<b>Category: </b>", data$offense_category,
"<br>", "<b>Description: </b>", data$offense_description,
"<br>", "<b>Day of week: </b>", data$day_of_week,
"<br>", "<b>Date: </b>", data$date,
"<br>", "<b>Time: </b>", data$incident_time,
"<br>", "<b>Neighborhood: </b>", data$neighborhood,
"<br>", "<b>Longitude: </b>", data$longitude,
"<br>", "<b>Latitude: </b>", data$latitude)
leaflet(data, width = "100%") %>% addTiles() %>%
addTiles(group = "OSM (default)") %>%
addProviderTiles(provider = "Esri.WorldStreetMap",group = "World StreetMap") %>%
#addProviderTiles(provider = "Esri.WorldImagery",group = "World Imagery") %>%
#addProviderTiles(provider = "NASAGIBS.ViirsEarthAtNight2012",group = "Nighttime Imagery") %>%
addMarkers(lng = ~longitude, lat = ~latitude, popup = data$popup, clusterOptions = markerClusterOptions()) %>%
addLayersControl(
baseGroups = c("OSM (default)","World StreetMap"),
options = layersControlOptions(collapsed = FALSE)
)